cmake_minimum_required(VERSION 3.10)
project(QtExamples VERSION 1.0.0)

# 1. 基础编译配置
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# -------------------------- 关键：找到 qmake6 路径（替代 shell 的 which qmake6） --------------------------
find_program(QMAKE6_EXECUTABLE NAMES qmake6)
if(NOT QMAKE6_EXECUTABLE)
    message(FATAL_ERROR "qmake6 not found! Please install Qt6 development tools.")
endif()

# -------------------------- 编译配置 --------------------------
add_executable(main main.cc)
target_include_directories(main PRIVATE /usr/include/x86_64-linux-gnu/qt6/)
target_link_directories(main PRIVATE /lib/x86_64-linux-gnu/)
target_link_libraries(main PRIVATE Qt6Widgets Qt6Core)

add_executable(main2 main2.cc)
target_include_directories(main2 PRIVATE
    /usr/include/x86_64-linux-gnu/qt6/
    /usr/include/gdk-pixbuf-2.0
    /usr/include/glib-2.0
    /usr/lib/x86_64-linux-gnu/glib-2.0/include
    /usr/include/libpng16
    /usr/include/x86_64-linux-gnu
    /usr/include/libmount
    /usr/include/blkid
)
target_link_directories(main2 PRIVATE /lib/x86_64-linux-gnu/)
target_link_libraries(main2 PRIVATE
    Qt6Widgets Qt6Core
    notify gdk_pixbuf-2.0 gio-2.0 gobject-2.0 glib-2.0
    pthread
)

# -------------------------- 修正：用 CMake 变量传递 qmake6 路径 --------------------------
add_custom_target(qtdeploy ALL
    COMMENT "Collecting Qt dependencies with linuxdeploy (Qt6)"
    DEPENDS main main2
    # 通过 CMake 变量 QMAKE6_EXECUTABLE 传递 qmake6 路径，避免 shell 语法错误
    COMMAND env QMAKE=${QMAKE6_EXECUTABLE} linuxdeploy --plugin qt -e $<TARGET_FILE:main> --appdir ${CMAKE_BINARY_DIR}/deps/main
    COMMAND env QMAKE=${QMAKE6_EXECUTABLE} linuxdeploy --plugin qt -e $<TARGET_FILE:main2> --appdir ${CMAKE_BINARY_DIR}/deps/main2
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    VERBATIM  # 确保命令参数被正确解析
)

# -------------------------- 安装规则 --------------------------
install(
    DIRECTORY ${CMAKE_BINARY_DIR}/deps/main/
    DESTINATION /usr/lib/qt-examples/main
    FILE_PERMISSIONS OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
    PATTERN "*.so*"
)
install(CODE "execute_process(COMMAND ln -sf /usr/lib/qt-examples/main/main /usr/bin/main)")

install(
    DIRECTORY ${CMAKE_BINARY_DIR}/deps/main2/
    DESTINATION /usr/lib/qt-examples/main2
    FILE_PERMISSIONS OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
    PATTERN "*.so*"
)
install(CODE "execute_process(COMMAND ln -sf /usr/lib/qt-examples/main2/main2 /usr/bin/main2)")

# 可选：桌面快捷方式
if(EXISTS ${CMAKE_SOURCE_DIR}/qt-examples.desktop)
    install(FILES ${CMAKE_SOURCE_DIR}/qt-examples.desktop
        DESTINATION /usr/share/applications
        PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
    )
endif()

# -------------------------- Debian 包配置 --------------------------
set(CPACK_GENERATOR "DEB")
set(CPACK_PACKAGE_NAME "qt-examples")
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
set(CPACK_PACKAGE_DESCRIPTION "Qt Example Programs: 
- main: Basic Qt window demo.
- main2: Qt + libnotify notification demo.
All Qt dependencies are bundled.")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Qt + libnotify examples (with bundled Qt dependencies)")
set(CPACK_PACKAGE_MAINTAINER "fuckerfucker <1690544550@qq.com>")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "fuckerfucker <1690544550@qq.com>")
set(CPACK_PACKAGE_ARCHITECTURE "amd64")
set(CPACK_DEBIAN_PACKAGE_SECTION "utils")
set(CPACK_DEBIAN_PACKAGE_DEPENDS
    "libnotify4 (>= 0.7.0),
     libglib2.0-0 (>= 2.64.0),
     libgdk-pixbuf2.0-0 (>= 2.40.0)"
)
set(CPACK_SET_DESTDIR ON)
set(CPACK_DEBIAN_FILE_NAME "${CPACK_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}_${CPACK_PACKAGE_ARCHITECTURE}.deb")

include(CPack)

# cmake_minimum_required(VERSION 3.10)
# project(QtExamples VERSION 1.0.0)

# # 1. 基础编译配置
# set(CMAKE_CXX_STANDARD 17)
# set(CMAKE_CXX_STANDARD_REQUIRED ON)
# set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# # -------------------------- 编译配置 --------------------------
# add_executable(main main.cc)
# target_include_directories(main PRIVATE /usr/include/x86_64-linux-gnu/qt6/)
# target_link_directories(main PRIVATE /lib/x86_64-linux-gnu/)
# target_link_libraries(main PRIVATE Qt6Widgets Qt6Core)

# add_executable(main2 main2.cc)
# target_include_directories(main2 PRIVATE
#     /usr/include/x86_64-linux-gnu/qt6/
#     /usr/include/gdk-pixbuf-2.0
#     /usr/include/glib-2.0
#     /usr/lib/x86_64-linux-gnu/glib-2.0/include
#     /usr/include/libpng16
#     /usr/include/x86_64-linux-gnu
#     /usr/include/libmount
#     /usr/include/blkid
# )
# target_link_directories(main2 PRIVATE /lib/x86_64-linux-gnu/)
# target_link_libraries(main2 PRIVATE
#     Qt6Widgets Qt6Core
#     notify gdk_pixbuf-2.0 gio-2.0 gobject-2.0 glib-2.0
#     pthread
# )

# # -------------------------- 关键修正：linuxdeploy 参数（--output-dir → --appdir） --------------------------
# add_custom_target(qtdeploy ALL
#     COMMENT "Collecting Qt dependencies with linuxdeploy..."
#     DEPENDS main main2
#     # 修正1：用 --appdir 指定输出目录（linuxdeploy 标准参数）
#     # 修正2：--executable 可简写为 -e，格式更简洁
#     COMMAND linuxdeploy --plugin qt -e $<TARGET_FILE:main> --appdir ${CMAKE_BINARY_DIR}/deps/main
#     COMMAND linuxdeploy --plugin qt -e $<TARGET_FILE:main2> --appdir ${CMAKE_BINARY_DIR}/deps/main2
#     WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
#     VERBATIM
# )

# # -------------------------- 安装规则 --------------------------
# # 安装 main 及其依赖
# install(
#     DIRECTORY ${CMAKE_BINARY_DIR}/deps/main/
#     DESTINATION /usr/lib/qt-examples/main
#     FILE_PERMISSIONS OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
#     PATTERN "*.so*"
# )
# install(CODE "execute_process(COMMAND ln -sf /usr/lib/qt-examples/main/main /usr/bin/main)")

# # 安装 main2 及其依赖
# install(
#     DIRECTORY ${CMAKE_BINARY_DIR}/deps/main2/
#     DESTINATION /usr/lib/qt-examples/main2
#     FILE_PERMISSIONS OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ
#     PATTERN "*.so*"
# )
# install(CODE "execute_process(COMMAND ln -sf /usr/lib/qt-examples/main2/main2 /usr/bin/main2)")

# # 可选：桌面快捷方式
# if(EXISTS ${CMAKE_SOURCE_DIR}/qt-examples.desktop)
#     install(FILES ${CMAKE_SOURCE_DIR}/qt-examples.desktop
#         DESTINATION /usr/share/applications
#         PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
#     )
# endif()

# # -------------------------- Debian 包配置 --------------------------
# set(CPACK_GENERATOR "DEB")
# set(CPACK_PACKAGE_NAME "qt-examples")
# set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
# set(CPACK_PACKAGE_DESCRIPTION "Qt Example Programs: 
# - main: Basic Qt window demo.
# - main2: Qt + libnotify notification demo.
# All Qt dependencies are bundled.")
# set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Qt + libnotify examples (with bundled Qt dependencies)")
# set(CPACK_PACKAGE_MAINTAINER "Your Name <your-email@example.com>")
# set(CPACK_PACKAGE_ARCHITECTURE "amd64")
# set(CPACK_DEBIAN_PACKAGE_SECTION "utils")
# set(CPACK_DEBIAN_PACKAGE_DEPENDS
#     "libnotify4 (>= 0.7.0),
#      libglib2.0-0 (>= 2.64.0),
#      libgdk-pixbuf2.0-0 (>= 2.40.0)"
# )
# set(CPACK_SET_DESTDIR ON)
# set(CPACK_DEBIAN_FILE_NAME "${CPACK_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}_${CPACK_PACKAGE_ARCHITECTURE}.deb")

# include(CPack)